//
//  CHAlertView.m
//  SZTZ
//
//  Created by 陈浩 on 2017/3/16.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHAlertView.h"

@interface CHAlertView ()
@property (nonatomic ,strong) UILabel *titleLabel;
@property (nonatomic ,strong) UILabel *contentLabel;
@property (nonatomic ,strong) UIButton *cancelButton;
@property (nonatomic ,strong) UIButton *otherButton;
@property (nonatomic ,strong) UIView *line;
@property (nonatomic ,copy) NSString *title;
@property (nonatomic ,copy) NSString *contentStr;
@property (nonatomic ,copy) NSString *cancelButtonTitle;
@property (nonatomic ,copy) NSString *otherButtonTitle;
@end

@implementation CHAlertView

-(instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitle{
    
    if (self = [super init]) {
        self.title = title;
        self.contentStr = message;
        self.cancelButtonTitle = cancelButtonTitle;
        self.otherButtonTitle = otherButtonTitle;
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        CHAlertView *alertView = [[CHAlertView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
        alertView.center = window.center;
        alertView.layer.cornerRadius = 5;
        alertView.layer.masksToBounds = YES;
        alertView.delegate = delegate;
        alertView.backgroundColor = [UIColor whiteColor];
        [window addSubview:alertView];
        
    }
    return self;
}


-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self initView];
    }
    return self;
    
    
}



-(void)initView{
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    NSLog(@"------->>>%@",self.title);
//    titleLabel.text = self.title;
    titleLabel.text = @"温馨提示";
    [self addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    UILabel *contentLabel = [[UILabel alloc] init];
    [self addSubview:contentLabel];
    contentLabel.text = self.contentStr;
    contentLabel.numberOfLines = 0;
    self.contentLabel = contentLabel;
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:line];
    
    UIButton *cancelButton = [[UIButton alloc] init];
    cancelButton.tag = 0;
    [cancelButton addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [cancelButton setTitle:self.cancelButtonTitle forState:UIControlStateNormal];
    [self addSubview:cancelButton];
    self.cancelButton = cancelButton;
    
    
    UIButton *otherButton = [[UIButton alloc] init];
    otherButton.tag = 1;
    [otherButton setTitle:self.otherButtonTitle forState:UIControlStateNormal];
    [otherButton addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:otherButton];
    self.otherButton = otherButton;
    
    
}

-(void)btnClick:(UIButton *)btn{
    
    if ([self.delegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)]) {
        [self.delegate alertView:self clickedButtonAtIndex:btn.tag];
    }
    
    
    
}


-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.titleLabel.frame = CGRectMake(0, 10, self.frame.size.width, 30);
    self.contentLabel.frame = CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame) +10, self.frame.size.width, 70);
    self.line.frame = CGRectMake(0, CGRectGetMaxY(self.contentLabel.frame), self.frame.size.width, 1);
    self.cancelButton.frame = CGRectMake(0, CGRectGetMaxY(self.line.frame), self.frame.size.width /2, 40);
    self.otherButton.frame = CGRectMake(self.frame.size.width/2, CGRectGetMaxY(self.line.frame), self.frame.size.width/2, 40);
    
}

@end
